<template>
  <div>
    <div class="dataList out-border d-flex justify-content-between layout-top">
      <div class="screen-l">
        <i class="el-icon-tickets"></i>
        <span class="txt">数据列表</span>
      </div>
      <el-button size="mini" @click="() => this.$router.push('/pms/addProduct')">添加</el-button>
    </div>
    <div class="layout-top">
      <el-table :data="productTypes" border style="width: 100%">
        <el-table-column prop="id" label="编号" width="100" align="center"> </el-table-column>
        <el-table-column prop="name" label="类型名称" width="370" align="center"> </el-table-column>
        <el-table-column prop="attributeCount" label="属性数量" width="200" align="center"> </el-table-column>
        <el-table-column prop="paramCount" label="参数数量" width="200" align="center"> </el-table-column>
        <el-table-column prop="address" label="设置" align="center" width="220">
          <template>
            <el-button size="mini">属性列表</el-button>
            <el-button size="mini">参数列表</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEditType(scope.row.name)">编辑</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="float: right" class="mt-4">
      <div class="block">
        <el-pagination
          :background="true"
          @size-change="getProTypeList"
          @current-change="getProTypeList"
          :current-page.sync="query.pageNum"
          :page-sizes="[5, 10, 15]"
          :page-size.sync="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog title="编辑类型" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <div>
        <el-form>
          <el-form-item label="类型名称" prop="name">
            <el-input v-model="DialogEditInfo"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fetchProductTypeListData } from '@/api/api'
import { removeProductItem } from '@/utils/utils'
export default {
  name: '',
  data() {
    return {
      dialogVisible: false,
      productTypes: [],
      total: 0,
      query: { pageNum: 1, pageSize: 5 },
      DialogEditInfo: '',
    }
  },
  methods: {
    async getProTypeList() {
      let res = await fetchProductTypeListData(this.query)
      let { list, total } = res.data
      this.total = total
      this.productTypes = list
    },
    open(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          removeProductItem(this.productTypes, id)
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 处理编辑类型弹窗
    handleEditType(name) {
      this.dialogVisible = true
      this.DialogEditInfo = name
    },
  },
  created() {
    this.getProTypeList()
  },
}
</script>
<style lang="scss" scoped>
.screen-l .txt {
  padding-left: 6px;
}
.dataList {
  padding: 18px 20px;
}
</style>
